<script lang="ts" setup name="XtxCarousel">
// 坑：script中不写东西 name会不起作用

import { BannerItem } from '@/types/data'
import { onMounted, onUnmounted, PropType, ref } from 'vue'

// 控制高亮的下标
const active = ref(0)

// 在公共组件中接受传下来的数据
// compositionApi结合ts给默认值，直接结构赋值
// const { slides, money = 100 } = defineProps<{
//   slides: BannerItem[]
//   money?: number
// }>()
// console.log(money)

const props = defineProps({
  slides: {
    type: Array as PropType<BannerItem[]>,
    require: true
  },
  autoplay: {
    type: Boolean,
    default: false
  },
  duration: {
    type: Number,
    default: 500
  }
})

// 点击左箭头
const handleClickOnPrev = () => {
  if (!props.slides?.length) return
  active.value--
  if (active.value < 0) {
    active.value = props.slides?.length - 1
  }
}

// 点击右箭头
const handleClickOnNext = () => {
  if (!props.slides?.length) return
  active.value++
  if (active.value > props.slides?.length - 1) {
    active.value = 0
  }
}

// 点击小圆点
const handleClickDot = (val: number) => {
  active.value = val
}

let timerId: number = -1
// 定时器逻辑，控制轮播图自动轮播
const timer = (): number => {
  // 在模块环境下编写代码时，定时器延时器，编辑器会识别成node语法，而我们需要的window的环境

  // if (!props.autoplay) return // 在次处使用auto使用play，表示永远都不会轮播
  return window.setInterval(() => {
    // console.log('轮播中')
    handleClickOnNext()
  }, props.duration)
}

onMounted(() => {
  // if (!props.autoplay) return // 在次处开启表示：进入首页不轮播，一旦用户鼠标移入之后轮播图之后，鼠标再移除就开始轮播

  // 开新的定时器之前先清除旧的
  clearInterval(timerId)
  timerId = timer()
})

onUnmounted(() => {
  // console.log('清除轮播')
  clearInterval(timerId)
})

// 鼠标移入暂停
const stop = () => {
  // console.log(123)
  clearInterval(timerId)
}

// 鼠标移除继续轮播
const paly = () => {
  clearInterval(timerId)
  timerId = timer()
}

</script>

<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="paly">

    <!-- 轮播主体 -->
    <ul class="carousel-body">
      <li v-for="item, index in slides" :key="item.id" class="carousel-item" :class="{ fade: active === index }">
        <RouterLink to="/">
          <img v-lazy="item.imgUrl" alt="" />
        </RouterLink>
      </li>

    </ul>

    <!-- 左右箭头 -->
    <a href="javascript:;" class="carousel-btn prev" @click="handleClickOnPrev">
      <i class="iconfont icon-angle-left"></i>
    </a>
    <a href="javascript:;" class="carousel-btn next" @click="handleClickOnNext">
      <i class="iconfont icon-angle-right"></i>
    </a>

    <!-- 小圆点 -->
    <div class="carousel-indicator">
      <span v-for="(item, index) in slides" :key="item.id" :class="{ active: active === index }"
        @mouseenter="handleClickDot(index)"></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;

  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }

    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;

      &.fade {
        opacity: 1;
        z-index: 1;
      }

      img {
        width: 100%;
        height: 100%;
      }
    }

    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;

      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;

        ~span {
          margin-left: 12px;
        }

        &.active {
          background: #fff;
        }
      }
    }

    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;

      &.prev {
        left: 20px;
      }

      &.next {
        right: 20px;
      }
    }
  }

  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>